<?php
function adminOnly($contents)
{
	$checker = CM_LOGINTABLE;
	
	if ($checker == 'none')
	{
		return '';
	}
	else
	{
		return adminContent($contents);
	}
}

function sqlDate($offset)
{
	$datevars = getdate();
	$dateToday=$datevars['year'].'-'.$datevars['mon'].'-'.$datevars['mday'];
	$newDate = strtotime($offset, strtotime($dateToday));
	$newDate = date ( 'Y-m-d' , $newDate );
	
	return $newDate;
}

function sqlDateNow()
{
	$datevars = getdate();
	$dateToday=$datevars['year'].'-'.$datevars['mon'].'-'.$datevars['mday'];
	$newDate = date ( 'Y-m-d' , strtotime($dateToday));
	return $newDate;
}

function doDML($query)
{
	$myarray = array();
	
	$db = new mysqli(CM_HOST, CM_USERNAME, CM_PASSWORD, CM_DBNAME);
	$result = $db->query($query);
	if(!$result)
	{
		$myarray[0] = false;
		$myarray[1] = "Failed to communicate with database. Entry was not saved successfully.";
	}
	else
	{
		$myarray[0] = true;
		$myarray[1] = "The query was saved successfully.";
	}
	
	return $myarray;
	
}


function doQuery($query)
{
	$myarray = array();
	
	$db = new mysqli(CM_HOST, CM_USERNAME, CM_PASSWORD, CM_DBNAME);
	$result = $db->query($query);
	if(!$result)
	{
		$myarray[0] = 'DB Query yielded no results';
		$myarray[1] = "Database error. Please check your SQL Statements again.";
	}
	else
	{
		$myarray[0] = 'DB Query success!';
		$numrows = $result->num_rows;
		for($x = 0; $x < $numrows; $x++)
		{
			$row = $result->fetch_assoc();
			if($x == 0)
			{
				$headers = implode("|", array_keys($row));
				$myarray[1] = $headers;
				$myarray[2] = implode("|", $row);
			}
			else
			{
				$myarray[] = implode("|", $row);				
			}
		}
	}
	return $myarray;
}

function tableSet($array)
{
	$query = '';
	for ($i=0; $i<sizeOf($array); $i++)
	{
		if($i == sizeOf($array)-1 )
		{
			$query .= ''.$array[$i].'';
		}
		else
		{
			$query .= ''.$array[$i].', ';
		}
	}
	
	return $query;
}

function fieldSet($array)
{
	$query = '';
	for ($i=0; $i<sizeOf($array); $i++)
	{
		if ($array[$i] == '*')
		{
			$query = '*';
		}
		else
		{
			if($i == sizeOf($array)-1 )
			{
				$query .= $array[$i];
			}
			else
			{
				$query .= $array[$i].', ';
			}
		}
	}
	
	return $query;
}

function dataSet($array)
{
	$query = '';
	for ($i=0; $i<sizeOf($array); $i++)
	{
		if($i == sizeOf($array)-1 )
		{
			$query .= '"'.$array[$i].'"';
		}
		else
		{
			$query .= '"'.$array[$i].'", ';
		}
	}
	return $query;
}

function validateWhere($whereClause)
{
	if ($whereClause == 'none')
	{
		return '';
	}
	else
	{
		return 'WHERE '.$whereClause;
	}
}


function SQLInsert($tableName, $fields, $values)
{
	$result = array();
	$query = 'INSERT INTO '.$tableName.' ('.fieldSet($fields).') VALUES ('.dataSet($values).');';
	$result = doDML($query);
	$display['queryStatus'] = $result[1];
	$display['traceSQL'] = $query;
	return $display;
}

function SQLRecover($tables, $fields, $whereClause)
{
	$result = array();
	$query = 'SELECT '.fieldSet($fields).' FROM '.tableSet($tables).' '.validateWhere($whereClause);
	$result = doQuery($query);
	
	$output = array();
	$output['queryStatus'] = $result[0];
	$output['traceSQL'] = $query;
	
	if (isset($result[1]))
	{
		$headers = explode("|", $result[1]);
		
		for ($h=0; $h<sizeOf($headers); $h++)
		{
			$output['dBheaders'][$h] = $headers[$h];
		}
		
		for ($i=2; $i<sizeOf($result); $i++)
		{
			$data = explode("|", $result[$i]);
			
			for ($j=0; $j<sizeOf($data); $j++)
			{
				$output[$headers[$j]][$i-2] = $data[$j];
			}
		}
	}
	else
	{
		$output['queryStatus'] = 'DB Success, no entries';
	}
	
	return $output;
	
	
}

function SQLDistinct($tables, $fields, $whereClause)
{
	$result = array();
	$query = 'SELECT DISTINCT '.fieldSet($fields).' FROM '.tableSet($tables).' '.validateWhere($whereClause);
	$result = doQuery($query);
	
	$output = array();
	$output['queryStatus'] = $result[0];
	$output['traceSQL'] = $query;
	
	if (isset($result[1]))
	{
		$headers = explode("|", $result[1]);
		
		for ($h=0; $h<sizeOf($headers); $h++)
		{
			$output['dBheaders'][$h] = $headers[$h];
		}
		
		for ($i=2; $i<sizeOf($result); $i++)
		{
			$data = explode("|", $result[$i]);
			
			for ($j=0; $j<sizeOf($data); $j++)
			{
				$output[$headers[$j]][$i-2] = $data[$j];
			}
		}
	}
	else
	{
		$output['queryStatus'] = 'DB Success, no entries';
	}
	
	return $output;
	
	
}

function SQLDelete ($tableName, $whereClause)
{
	$query = 'DELETE FROM '.$tableName.' '.validateWhere($whereClause).';';
	$result = doDML($query);
	$display['queryStatus'] = $result[1];
	$display['traceSQL'] = $query;
	$display['boolResult'] = $result[0];
	return $display;
}

function SQLUpdate($tableName, $whereClause, $fields, $values)
{
	if ( sizeOf($fields) != sizeOf($values))
	{
		$display = array();
		$display['queryStatus'] = '$field size does not match $value size! Please reconsider!';
		$display['traceSQL'] = 'The SQL Statement was not created. Echo \'queryStatus\' to view the situation';
		return 	$display;
	}
	else
	{
		$changes = '';
		for ($i=0; $i<sizeOf($fields); $i++)
		{
			if ($i == sizeOf($fields) - 1)
			{
				$changes .= $fields[$i].'="'.$values[$i].'"';
			}
			else
			{
				$changes .= $fields[$i].'="'.$values[$i].'", ';
			}
		}
		$query = 'UPDATE '.$tableName.' SET '.$changes.' '.validateWhere($whereClause).';';
		$result = array();
		$result = doDML($query);
		$display = array();
		$display['queryStatus'] = $result[1];
		$display['traceSQL'] = $query;
		
		return $display;
	}
}

function getData($table, $field, $where)
{
	$tables = array();
	$tables[0] = $table;
	
	$fields = array();
	$fields[0] = $field;
	
	$whereClause = $where;
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	if($data['queryStatus'] != 'DB Query success!')
	{
		return 'null';
	}
	else
	{
		return $data[$field][0];
	}
	return 'null';
}
?>